<template>
    <span>
        <RadioGroup v-model="sel" @on-change='selectChange'>
            <template v-for="item in list">
                <Radio :label="item.code">{{item.name}}</Radio>
            </template>
        </RadioGroup>
    </span>
</template>
<script>
    import {GET_STATUS,GET_FAR_STATUS,GET_LAZY_STATUS} from 'serves/data-status'
    export default{
        name:'StatusRadio',
        props:{
            pData:{
                type:[Number,String],
                request:true
            },
            pName:{
                type:String,
                request:true
            },
            pKeyText:{
                type:String,
                default:'code'
            },
            pValueText:{
                type:String,
                default:'name'
            },
            pLazy:{
                type:Boolean,
                default:false
            },
            pDef:{
                type:Boolean,
                default:false
            },
            pFar:{
                type:Boolean,
                default:false
            },
            pDel:{
                type:Array,
                default:function () {
                    return [];
                }
            },
        },
        data(){
            return{
                text:'',
                sel:'',
                list:[],
            }
        },
        computed:{
        },
        created(){
            this.init();
        },
        methods:{
            initSel(){
                this.$set(this.$data,'sel',this.pData);
            },
            initList(d){
                if(this.pDel.length>0){
                    d = d.filter((v,i)=>{
                        let z = true;
                        this.pDel.forEach((x,y)=>{
                            if(v[this.pKeyText]==x){
                                z = false;
                            }
                        });
                        return z;
                    });
                }
                this.$set(this.$data,'list',d);
            },
            init(){
                if(this.pFar){
                    let d = GET_FAR_STATUS(this.pName,false,this.pKeyText);
                    this.initList(d);
                    this.defChange();
                }else {
                    if(!this.pLazy){
                        let d = GET_STATUS(this.pName,false,this.pKeyText);
                        this.initList(d);
                        this.defChange()
                    }else {
                        GET_LAZY_STATUS(this.pName,false,this.pKeyText).then((res)=>{
                            this.initList(res);
                            this.defChange()
                        });
                    }
                }
            },
            selectChange(value){
                this.$emit('on-change',value)
            },
            defChange(){
                if(this.pDef&&this.pData === ''){
                    this.selectChange(this.list[0]&&this.list[0][this.pKeyText])
                }else{
                    this.initSel();
                }
            }
        },
        watch:{
            pData(newVal,olVal){
                if(newVal!=this.sel){
                    this.initSel()
                }
                if(newVal==''){
                    this.defChange();
                }
            }
        },
        components:{

        }
    }
</script>